
const tokens = {
  admin: {
    token: 'admin-token'
  },
  editor: {
    token: 'editor-token'
  }
}

const users = {
  'admin-token': {
    roles: ['admin'],
    introduction: 'I am a super administrator',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: 'Super Admin',
    rg: ''
  },
  'editor-token': {
    roles: ['editor'],
    introduction: 'I am an editor',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: 'Normal Editor'
  }
}

// const mockData = [
//   { id: 1, name: '苹果', day: '2025年2月7日', jinhuo: '北京', kucun: '888', maichu: '99单' },
//   { id: 2, name: '香蕉', day: '2025年2月8日', jinhuo: '上海', kucun: '666', maichu: '50单' },
//   { id: 3, name: '橙子', day: '2025年2月9日', jinhuo: '广州', kucun: '777', maichu: '80单' }
// ]

const fs = require('fs')
const path = require('path')
const dbPath = path.join(__dirname, 'db.json')

// 读取db.json数据
function readData() {
  const rawData = fs.readFileSync(dbPath)
  return JSON.parse(rawData)
}

// 写入db.json数据
function writeData(data) {
  fs.writeFileSync(dbPath, JSON.stringify(data, null, 2))
}

module.exports = [
  // user login
  {
    url: '/vue-element-admin/user/login',
    type: 'post',
    response: config => {
      const { username } = config.body
      const token = tokens[username]

      if (!token) {
        return {
          code: 60204,
          message: '账号不存在或密码错误'
        }
      }

      return {
        code: 20000,
        data: {
          token: { token: 'admin-token' },
          roles: ['admin'],
          introduction: 'I am a super administrator',
          avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
          name: 'Super Admin'
        }
      }
    }
  },

  // get user info
  {
    url: '/vue-element-admin/user/info\.*',
    type: 'get',
    response: config => {
      const { token } = config.query
      const info = users[token]

      // mock error
      if (!info) {
        return {
          code: 50008,
          message: 'Login failed, unable to get user details.'
        }
      }

      return {
        code: 20000,
        data: info
      }
    }
  },

  // user logout
  {
    url: '/vue-element-admin/user/logout',
    type: 'post',
    response: _ => {
      return {
        code: 20000,
        data: 'success'
      }
    }
  },

  {
    url: '/vue-element-admin/user/register',
    type: 'post',
    response: config => {
      const { mobile } = config.body

      // 检查手机号是否已注册
      if (tokens[mobile]) {
        return {
          code: 60205,
          message: '该手机号已注册'
        }
      }

      // 创建新用户
      const newToken = mobile + '-token'
      tokens[mobile] = { token: newToken }
      users[newToken] = {
        roles: ['user'],
        introduction: '新注册用户',
        avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
        name: mobile
      }

      return {
        code: 20000,
        message: '注册成功',
        data: { token: newToken }
      }
    }
  },
  // 添加组数据模拟接口
  // {
  //   url: '/vue-element-admin/group/data',
  //   type: 'get',
  //   response: _ => {
  //     return {
  //       code: 20000,
  //       data: [
  //         { id: 1, name: '苹果', day: '2025年2月7日', jinhuo: '北京', kucun: '888', maichu: '99单' },
  //         { id: 2, name: '香蕉', day: '2025年2月8日', jinhuo: '上海', kucun: '666', maichu: '50单' },
  //         { id: 3, name: '橙子', day: '2025年2月9日', jinhuo: '广州', kucun: '777', maichu: '80单' }
  //       ]
  //     }
  //   }
  // },
  // {
  //   url: '/vue-element-admin/group/create',
  //   type: 'post',
  //   response: config => {
  //     return {
  //       code: 20000,
  //       message: '创建成功',
  //       data: config.body
  //     }
  //   }
  // },
  // {
  //   url: '/vue-element-admin/group/update',
  //   type: 'post',
  //   response: config => {
  //     return {
  //       code: 20000,
  //       message: '更新成功',
  //       data: config.body
  //     }
  //   }
  // },
  // {
  //   url: '/vue-element-admin/group/delete',
  //   type: 'post',
  //   response: config => {
  //     return {
  //       code: 20000,
  //       message: '删除成功',
  //       data: config.body
  //     }
  //   }
  // }

  // 获取组数据
  // {
  //   url: '/vue-element-admin/group/data',
  //   type: 'get',
  //   response: _ => {
  //     return {
  //       code: 20000,
  //       data: mockData
  //     }
  //   }
  // },

  // // 创建组数据
  // {
  //   url: '/vue-element-admin/group/create',
  //   type: 'post',
  //   response: config => {
  //     const newItem = {
  //       ...config.body,
  //       id: mockData.length + 1

  //     }
  //     console.log(config.body)
  //     mockData.push(newItem)
  //     // console.log(mockData)

  //     return {
  //       code: 20000,
  //       message: '创建成功',
  //       data: newItem
  //     }
  //   }
  // },

  // // 更新组数据
  // {
  //   url: '/vue-element-admin/group/update',
  //   type: 'post',
  //   response: config => {
  //     const index = mockData.findIndex(item => item.id === config.body.id)
  //     if (index >= 0) {
  //       mockData[index] = {
  //         ...mockData[index],
  //         ...config.body
  //       }
  //       return {
  //         code: 20000,
  //         message: '更新成功',
  //         data: mockData[index]
  //       }
  //     }
  //     return {
  //       code: 50000,
  //       message: '未找到对应数据'
  //     }
  //   }
  // },

  // // 删除组数据
  // {
  //   url: '/vue-element-admin/group/delete',
  //   type: 'post',
  //   response: config => {
  //     const index = mockData.findIndex(item => item.id === config.body.id)
  //     if (index >= 0) {
  //       mockData.splice(index, 1)
  //       return {
  //         code: 20000,
  //         message: '删除成功'
  //       }
  //     }
  //     return {
  //       code: 50000,
  //       message: '未找到对应数据'
  //     }
  //   }
  // }
  // 获取组数据
  {
    url: '/vue-element-admin/group/data',
    type: 'get',
    response: _ => {
      const db = readData()
      return {
        code: 20000,
        data: db.mockData
      }
    }
  },

  // 创建组数据
  {
    url: '/vue-element-admin/group/create',
    type: 'post',
    response: config => {
      const db = readData()
      const newItem = {
        ...config.body,
        id: db.mockData.length + 1
      }

      db.mockData.push(newItem)
      writeData(db)

      return {
        code: 20000,
        message: '创建成功',
        data: newItem
      }
    }
  },

  // 更新组数据
  {
    url: '/vue-element-admin/group/update',
    type: 'post',
    response: config => {
      const db = readData()
      const index = db.mockData.findIndex(item => item.id === config.body.id)

      if (index >= 0) {
        db.mockData[index] = {
          ...db.mockData[index],
          ...config.body
        }
        writeData(db)

        return {
          code: 20000,
          message: '更新成功',
          data: db.mockData[index]
        }
      }

      return {
        code: 50000,
        message: '未找到对应数据'
      }
    }
  },

  // 删除组数据
  {
    url: '/vue-element-admin/group/delete',
    type: 'post',
    response: config => {
      const db = readData()
      const index = db.mockData.findIndex(item => item.id === config.body.id)

      if (index >= 0) {
        db.mockData.splice(index, 1)
        writeData(db)

        return {
          code: 20000,
          message: '删除成功'
        }
      }

      return {
        code: 50000,
        message: '未找到对应数据'
      }
    }
  },
  // 卖出
  // {
  //   url: '/vue-element-admin/sell/data',
  //   type: 'post',
  //   response: req => {
  //     const { name } = JSON.parse(req.body)
  //     const db = readData()

  //     // 检查maichu数组中是否已有该商品
  //     const existingItem = db.maichu.find(item => item.name === name)

  //     if (existingItem) {
  //       // 如果存在则增加销量
  //       existingItem.count = (existingItem.count || 1) + 1
  //     } else {
  //       // 如果不存在则新增
  //       const newItem = {
  //         name,
  //         count: 1,
  //         date: new Date().toLocaleDateString()
  //       }
  //       db.maichu.push(newItem)
  //     }

  //     // 写入文件
  //     writeData(db)

  //     return {
  //       code: 20000,
  //       data: db.maichu
  //     }
  //   }
  // }
  // 卖出
  {
    url: '/vue-element-admin/fruit/sell',
    type: 'post',
    response: config => {
      const db = readData()
      const { id, sellNum } = config.body
      const index = db.mockData.findIndex(item => item.id === id)

      if (index >= 0) {
        const sellNumValue = Number(sellNum)
        const currentKucun = Number(db.mockData[index].kucun)
        const currentMaichu = Number(db.mockData[index].maichu)
        const price = Number(db.mockData[index].price) || 0

        db.mockData[index].kucun = currentKucun - sellNumValue
        db.mockData[index].maichu = currentMaichu + sellNumValue
        db.mockData[index].shouyi = (Number(db.mockData[index].shouyi) || 0) + (price * sellNumValue)

        writeData(db)

        return {
          code: 20000,
          data: {
            id: db.mockData[index].id,
            name: db.mockData[index].name,
            kucun: db.mockData[index].kucun,
            maichu: db.mockData[index].maichu,
            shouyi: db.mockData[index].shouyi
          },
          message: '销售数据更新成功'
        }
      }

      return {
        code: 50000,
        message: '未找到对应水果'
      }
    }
  },

  // 卖出记录
  // ... existing code ...

  // 卖出记录接口
  {
    url: '/vue-element-admin/maichu/record',
    type: 'post',
    response: config => {
      const recordData = config.body
      const dbData = readData()

      // 检查是否已存在相同记录
      const existingIndex = dbData.maichu.findIndex(
        item => item.name === recordData.name
      )

      if (existingIndex >= 0) {
        // 替换已有记录
        dbData.maichu[existingIndex] = recordData
      } else {
        // 添加新记录
        dbData.maichu.push(recordData)
      }

      writeData(dbData)

      return {
        code: 20000,
        message: '操作成功',
        data: recordData
      }
    }
  },

  // 添加maichu数据获取接口
  {
    url: '/vue-element-admin/maichu/data',
    type: 'get',
    response: _ => {
      const data = readData()
      return {
        code: 20000,
        data: data.maichu
      }
    }
  },

  {
    url: '/vue-element-admin/user/update',
    type: 'post',
    response: config => {
      const { user } = config.body

      // 读取当前数据
      const dbData = readData()

      // 更新用户数据
      dbData.user[0].user = user

      // 写回文件
      writeData(dbData)

      return {
        code: 20000,
        message: '用户数据更新成功',
        data: dbData.user[0]
      }
    }
  },

  // 添加获取用户数据的接口
  {
    url: '/vue-element-admin/user/data',
    type: 'get',
    response: () => {
      const dbData = readData()
      return {
        code: 20000,
        data: dbData.user[0]
      }
    }
  }

// ... existing code ...
]

